/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package its.serviceSystem.Bean;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dds2.ServicesSystem.Modelo.Tarrain;
import mx.its.dds2.ServicesSystem.Modelo.Usersystem;
import mx.its.dds2.serviceSystem.Bl.TerrainBlLocal;
import mx.its.dds2.serviceSystem.Dao.UsersystemDaoLocal;

/**
 *
 * @author Carlos Asis
 */
@ManagedBean
@SessionScoped
public class TerrainBean {

    @EJB
    private UsersystemDaoLocal usersystemDao;

    /**
     * Creates a new instance of TerrainBean
     */
    public TerrainBean() {
        terrain = new Tarrain();
    }
    @EJB
    private TerrainBlLocal terrainBl;
    private Tarrain terrain;
    private List<Tarrain> listTarrains;
    private List<Usersystem> listUser;
    private int userSelect;
    private int b = 0;

    public Tarrain getTarrain() {
        return terrain;
    }

    public int getUserSelect() {
        return userSelect;
    }

    public List<Usersystem> getListUser() {
        listUser = usersystemDao.findAll();
        return listUser;
    }

    public void setListUser(List<Usersystem> listUser) {
        this.listUser = listUser;
    }

    public void setUserSelect(int userSelect) {
        this.userSelect = userSelect;
    }

    public void setTarrain(Tarrain terrain) {
        this.terrain = terrain;
    }

    public List<Tarrain> getTarrains() {
        listTarrains = terrainBl.getListAll();
        return listTarrains;
    }

    public void setTarrains(List<Tarrain> listTarrains) {
        this.listTarrains = listTarrains;
    }

    public String prepararNuevo() {
        b = 1;
        terrain = new Tarrain();
        return "TerrainCrearEditar";
    }

    public void prepararEditar(ActionEvent a) {
        b = 2;
        String cId = a.getComponent().getClientId().split(":")[2];
        int id = Integer.parseInt(cId);
        terrain = listTarrains.get(id);
    }

    public String guardar() {
        switch (b) {
            case 1:
                terrainBl.register(terrain);
                break;
            case 2:
                terrainBl.modify(terrain);
                break;
            case 3:
            // terrainBl.r(terrain);
        }
        return "TerrainFacelets";
    }

    public void eliminar(ActionEvent a) {
        b = 3;
        String cId = a.getComponent().getClientId().split(":")[2];
        int id = Integer.parseInt(cId);
        terrain = listTarrains.get(id);
        guardar();
    }
    public void prepararUser(ActionEvent evet) {
        terrain.setIdUser(userSelect);
    }
}
